﻿using System.Reflection;
using System.Runtime.Versioning;

namespace DBUtility.Standard
{
    /// <summary>
    /// 
    /// </summary>
    public class AssemblyHelper
    {
        /// <summary>
        /// 获取应用程序框架
        /// </summary>
        /// <returns></returns>
        public static string GetFramework()
        {
            string frameworkName = "";
            // 获取当前程序集的 TargetFrameworkAttribute
            var attributes = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(TargetFrameworkAttribute), false);
            if (attributes.Length > 0)
            {
                var frameworkAttribute = (TargetFrameworkAttribute)attributes[0];
                if (frameworkAttribute != null)
                {
                    frameworkName = frameworkAttribute.FrameworkName;
                }
            }
            return frameworkName;
        }

        /// <summary>
        /// 是否.NET Framework框架
        /// </summary>
        /// <returns></returns>
        public static bool IsNetFramework()
        {
            string frameworkName = GetFramework();
            if (!string.IsNullOrEmpty(frameworkName))
            {
                if (frameworkName.StartsWith(".NETFramework"))
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 是否.NET Core框架
        /// </summary>
        /// <returns></returns>
        public static bool IsNetCore()
        {
            string frameworkName = GetFramework();
            if (!string.IsNullOrEmpty(frameworkName))
            {
                if (frameworkName.StartsWith(".NETCoreApp"))
                {
                    return true;
                }
            }
            return false;
        }
    }
}
